package state.component;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import state.Control;

import development.Global;

public class SystemTextField extends Form {
  private Command cancelCommand;
  private Command okCommand;
  
  private TextField titleTextField;
  private state.component.TextField parent;

  public SystemTextField(state.component.TextField parent) {
    super("");
    this.parent = parent;
    titleTextField = new TextField("", parent.getText(), parent.getMaxLength(), TextField.ANY);
    
    cancelCommand = new Command("Hủy", Command.EXIT, 1);
    okCommand = new Command("Xong", Command.OK, 1);
    append(titleTextField);
    addCommand(okCommand);
    addCommand(cancelCommand);
    setCommandListener(new CommandListener() {
      public void commandAction(Command c, Displayable d) {
        if (c == okCommand) {
          SystemTextField.this.parent.setText(titleTextField.getString());
          SystemTextField.this.parent.setFocused(true);
        }
        
        new Control() {
          public void perform() {
            try {
              sleep(500);
            } catch (InterruptedException e) {
            }
            Display.getDisplay(Global.systemCanvas.getMidlet()).setCurrent(Global.systemCanvas);
            Global.systemCanvas.setFullScreenMode(true);
          }
        }.start();
      }
    });
  }
  
  public void refesh() {
    titleTextField.setString(parent.getText());
  }
  
  public void updateData() {
    titleTextField.setString(parent.getText());
    titleTextField.setMaxSize(parent.getMaxLength());
    titleTextField.setConstraints(parent.getConstraint());
  }
}
